/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.activity.Enum.AgreementStatusEnum;
import com.ykht.proj.activity.vo.VBrandAgreementVo;
import com.ykht.proj.admin.api.feign.RemoteUserService;
import com.ykht.proj.admin.api.vo.UserVO;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.activity.entity.VBrandAgreement;
import com.ykht.proj.activity.service.VBrandAgreementService;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.CnShop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 品牌协议表
 *
 * @author csg
 * @date 2021-05-18 14:25:31
 */
@RestController
@AllArgsConstructor
@RequestMapping("/vbrandagreement")
@Api(value = "vbrandagreement", tags = "品牌协议表管理")
@Slf4j
public class VBrandAgreementController {

	private final VBrandAgreementService vBrandAgreementService;

	private final RemoteUserService remoteUserService;

	/**
	 * 品牌协议分页查询
	 *
	 * @param page            分页对象
	 * @param vBrandAgreement 品牌协议表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
//	@PreAuthorize("@pms.hasPermission('activity_vbrandagreement_view')")
	public R getVBrandAgreementPage(Page page, VBrandAgreementVo vBrandAgreementVo) {
		log.info("->品牌协议 分页查询{}", vBrandAgreementVo);
		IPage<VBrandAgreementVo> dtoiPage = vBrandAgreementService.getBrandAgreementListPage(page, vBrandAgreementVo);
		return R.ok(dtoiPage);
	}


	/**
	 * 通过id查询品牌协议表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreement_view')")
	public R<VBrandAgreementVo> getById(@PathVariable("id") Long id) {
		VBrandAgreementVo vBrandAgreementVo = vBrandAgreementService.getByIdBrandAgreement(id);
		if (null != vBrandAgreementVo.getLoginId()) {
			UserVO byIdUser = remoteUserService.getByIdUser(vBrandAgreementVo.getLoginId());
			vBrandAgreementVo.setUsername(byIdUser.getUsername());
			vBrandAgreementVo.setPhone(byIdUser.getPhone());
		}
		return R.ok(vBrandAgreementVo);
	}

	/**
	 * 新增品牌协议表
	 *
	 * @param vBrandAgreement 品牌协议表
	 * @return R
	 */
	@ApiOperation(value = "新增品牌协议表", notes = "新增品牌协议表")
	@SysLog("新增品牌协议表")
	@PostMapping
//    @PreAuthorize("@pms.hasPermission('activity_vbrandagreement_add')" )
	public R save(@RequestBody VBrandAgreement vBrandAgreement) {
		log.info("->新增品牌协议表{}", vBrandAgreement);
		if (vBrandAgreement.getEndTime().isBefore(LocalDateTime.now())) {
			return R.failed("协议结束时间要大于当前日期");
		}
		if (LocalDateTime.now().isBefore(vBrandAgreement.getStaTime())) {
			vBrandAgreement.setAgreementStatus(AgreementStatusEnum.TO_START.getValue());
		} else {
			vBrandAgreement.setAgreementStatus(AgreementStatusEnum.AT_WORK.getValue());
		}
		vBrandAgreementService.saveAndRenewal(vBrandAgreement);
		return R.ok();
	}

	/**
	 * 添加续约品牌协议表
	 *
	 * @return R
	 */
	@ApiOperation(value = "添加续约品牌协议表")
	@SysLog("添加续约品牌协议表")
	@PostMapping("addRenewalAgreement")
	public R addRenewalAgreement(@RequestBody VBrandAgreementVo vBrandAgreementVo) {
		log.info("->添加续约品牌协议表{}", vBrandAgreementVo);
		if (vBrandAgreementVo.getEndTime().isBefore(LocalDateTime.now())) {
			return R.failed("协议结束时间要大于当前日期");
		}
		if (null == vBrandAgreementVo.getStaTime()){
			VBrandAgreement agreement = vBrandAgreementService.getById(vBrandAgreementVo.getId());
			if (LocalDateTime.now().isBefore(agreement.getStaTime())) {
				vBrandAgreementVo.setAgreementStatus(AgreementStatusEnum.TO_START.getValue());
			} else {
				vBrandAgreementVo.setAgreementStatus(AgreementStatusEnum.AT_WORK.getValue());
			}
		}
		if (null != vBrandAgreementVo.getStaTime()){
			if (LocalDateTime.now().isBefore(vBrandAgreementVo.getStaTime())) {
				vBrandAgreementVo.setAgreementStatus(AgreementStatusEnum.TO_START.getValue());
			} else {
				vBrandAgreementVo.setAgreementStatus(AgreementStatusEnum.AT_WORK.getValue());
			}
		}
		vBrandAgreementService.updateAndRenewal(vBrandAgreementVo);
		return R.ok();
	}


	/**
	 * 修改品牌协议表
	 *
	 * @param vBrandAgreement 品牌协议表
	 * @return R
	 */
	@ApiOperation(value = "修改品牌协议表", notes = "修改品牌协议表")
	@SysLog("修改品牌协议表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreement_edit')")
	public R updateById(@RequestBody VBrandAgreement vBrandAgreement) {
		return R.ok(vBrandAgreementService.updateById(vBrandAgreement));
	}


	/**
	 * 协议暂停/恢复
	 *
	 * @return R
	 */
	@ApiOperation(value = "协议暂停/恢复")
	@SysLog("协议暂停/恢复")
	@PostMapping("updateRenewalAgreement")
	public R updateRenewalAgreement(@RequestBody VBrandAgreement vBrandAgreement) {
		log.info("->协议暂停/恢复{}", vBrandAgreement);
		vBrandAgreement.setUpdateUser(SecurityUtils.getUserId());
		vBrandAgreement.setUpdateTime(LocalDateTime.now());
		return R.ok(vBrandAgreementService.updateById(vBrandAgreement));
	}


	/**
	 * 通过id删除品牌协议表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除品牌协议表", notes = "通过id删除品牌协议表")
	@SysLog("通过id删除品牌协议表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('activity_vbrandagreement_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(vBrandAgreementService.removeById(id));
	}


	@ApiOperation(value = "校验品牌是否存在")
	@PostMapping("/checkPlanName")
	public R checkSjName(@RequestBody VBrandAgreement vBrandAgreement) {
		log.info("->校验品牌是否存在{}", vBrandAgreement);
		VBrandAgreement brandAgreement = new VBrandAgreement().selectOne(Wrappers.lambdaQuery(VBrandAgreement.class)
				.eq(VBrandAgreement::getBrandId, vBrandAgreement.getBrandId()));
		return R.ok(brandAgreement == null ? true : brandAgreement.getId().equals(vBrandAgreement.getId()) ? true : false);
	}


	@ApiOperation("根据品牌id集合获取协议列表")
	@PostMapping("/getByBrandIdList")
	public List<VBrandAgreement> getByBrandIdList(@RequestBody List<Long> brandIdList) {
		return vBrandAgreementService.list(new QueryWrapper<VBrandAgreement>().lambda().in(VBrandAgreement::getBrandId, brandIdList));
	}

}
